# Maintainer: LH_Mouse <lh_mouse@126.com>

_realname=mcfgthread
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}"-libs)
pkgver=1.4.1
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/lhmouse/mcfgthread"
license=('spdx:LGPL-3.0-or-later' 'custom')
options=('staticlibs' 'strip' '!buildflags')
_commit='66326556e26f07872c797bbdbea5e398de32e66d'
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-crt"
  "${MINGW_PACKAGE_PREFIX}-headers"
  "git"
)
source=("git+https://github.com/lhmouse/mcfgthread.git#commit=${_commit}")
sha256sums=('SKIP')

prepare() {
  cd "${srcdir}/${_realname}"

  mkdir -p m4
  autoreconf -ifv

  pushd test_c++
  mkdir -p m4
  autoreconf -ifv
  popd
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  export CFLAGS+=' -Os -g'

  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-pch

  make

  rm -rf "${srcdir}${MINGW_PREFIX}"
  make -j1 DESTDIR=${srcdir} install
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check

  # Run c++ tests, which have to be configured separately after C
  # libraries have been built.
  mkdir -p test_c++
  pushd test_c++

  ../../${_realname}/test_c++/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST}

  make check
  popd
}

package_mcfgthread() {
  pkgdesc="An efficient gthread implementation, providing C++11 threading support (devel)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}"-libs)

  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  cd "${srcdir}${MINGW_PREFIX}"
  _files_cp="$(find bin -type f -and -not -name "*.dll")"
  [[ -z "${_files_cp}" ]] || cp --parents -r ${_files_cp} --target-directory="${pkgdir}${MINGW_PREFIX}"
  cp --parents -r include   --target-directory="${pkgdir}${MINGW_PREFIX}"
  cp --parents -r lib       --target-directory="${pkgdir}${MINGW_PREFIX}"
}

package_mcfgthread-libs() {
  pkgdesc="An efficient gthread implementation, providing C++11 threading support (libs)"

  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  cd "${srcdir}${MINGW_PREFIX}"
  _files_cp="$(find bin -type f -and -name "*.dll")"
  [[ -z "${_files_cp}" ]] || cp --parents -r ${_files_cp} --target-directory="${pkgdir}${MINGW_PREFIX}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
